<?php

define(SIZEMULTIPLE, 2000);

function draw24htable($country, $regionid, $dbh) {
	$scandates = array();
	$query = mysql_query("SELECT * FROM scans where ScanID >= {$country['VeryFirstScan']} and ScanID <= {$country['VeryLastScan']} ORDER BY ScanID ASC", $dbh);
	while ($data = mysql_fetch_array($query)) {
		$scandates[$data['ScanID']] = $data['start'];
	}
	foreach (array_keys($country['Regions']) as $region) {
		$country['Regions'][$region] = interpolate_missing_scans($country['Regions'][$region], $country['VeryFirstScan'], $country['VeryLastScan']);
	}
	echo "<B>Analysis of user activity over the past 24 hours</B><BR/>\n";
	echo "Scale: <IMG SRC=\"b.png\" WIDTH=\"" . SIZEMULTIPLE / 10 . "\" HEIGHT=\"15\"> = 10% of total users in league or region; this means that 1000 users in a small league gives a bigger bar than 1000 users in a large league.<BR/>\n";
	echo "<span style=\"background: silver\">Gray bars</span> indicate that (some) data was missing and that the values have been partly or entirely interpolated.<br/>\n";
	echo "<TABLE>\n";
	echo "\t<TR>";
	for($i = $country['VeryFirstScan']; $i < $country['VeryLastScan']; $i++) {
		echo "<TD>". date("H", strtotime($scandates[$i]))."</TD>";
	}
	echo "<TD COLSPAN=\"2\">" . date("H:i:s", strtotime($scandates[$country['VeryLastScan']])) . "</TD></TR>\n";
	
	$world = getworldarray($dbh);
	foreach (array_keys($world['Regions']) as $region) {
		$world['Regions'][$region] = interpolate_missing_scans($world['Regions'][$region], $world['VeryFirstScan'], $world['VeryLastScan']);
	}
	
	draw24hcountryline($world, $world['VeryFirstScan'], $world['VeryLastScan']);
	
	draw24hcountryline($country, $world['VeryFirstScan'], $world['VeryLastScan']);
	
	if ($regionid > 0) draw24hregionline($country['Regions'][$regionid], $world['VeryFirstScan'], $world['VeryLastScan']);
	
	echo "</TABLE>\n";
	
}

function draw24hcountryline ($country, $firstscan, $lastscan) {
	$heights = array();
	$values = array();
	$interpolations = array();
	
	for ($i = $firstscan; $i <= $lastscan; $i++) {
		$totals = 0;
		$interpolated = false;
		foreach ($country['Regions'] as $region) {
			$totals += $region['Scans'][$i]['NumberOfOnline'];
			if ($region['Scans'][$i]['Interpolated']) $interpolated = true;
		}
		if ($country['ActiveUsers'] > 0) {
			$heights[] = round(($totals/$country['ActiveUsers']) * SIZEMULTIPLE )+1;
		} else {
			$heights[] = 1;	
		}
		$values[] = $totals;
		$interpolations[] = $interpolated;
	}
	draw24hline($country['LeagueName'], $heights, $values, $interpolations);
}


function draw24hregionline($region, $firstscan, $lastscan) {
		$heights = array();
		$values = array();
		$interpolations = array();
		for ($i = $firstscan; $i <= $lastscan; $i++) {
			if ($region['Scans'][$i]['NumberOfUsers'] > 0) {
				$heights[] = round(($region['Scans'][$i]['NumberOfOnline']/$region['Scans'][$i]['NumberOfUsers']) * SIZEMULTIPLE)+1;
				$values[] = $region['Scans'][$i]['NumberOfOnline'];
			} else {
				$heights[] = 1;
				$values[] = 0;
			}
			if ($region['Scans'][$i]['Interpolated']) {
				$interpolations[] = true;
			} else {
				$interpolations[] = false;
			}
		}
		draw24hline($region['RegionName'], $heights, $values, $interpolations);
}

function draw24hline($linename, $heights, $values, $interpolations) {
	
	echo "\t<TR>";
	for ($i = 0; $i < count($heights); $i++) {
		if ($interpolations[$i]) $interpolation = "g";
		else $interpolation = "b";
		echo "<TD VALIGN=\"bottom\" class=\"graph\">{$values[$i]}<BR/><IMG SRC=\"$interpolation.png\" WIDTH=\"22\" HEIGHT=\"{$heights[$i]}\"/></TD>";
	}
	echo "<TD><B>$linename</B></TD></TR>\n";
}
?>